﻿
package org.deft.tween {

	internal class DTweenProp extends Object {
		//用于动画的缓动函数
		public var easeFun:Function = null;

		//read-only 完成率
		private var _completeRate:Number = 0;

		public function get completeRate():Number {
			return _completeRate;
		}

		/**
		 * 默认的缓动函数
		 */
		private static var easeDefault:Function = function(t:Number, b:Number, c:Number, d:Number):Number {
			return c * t / d + b;
		};

		private var _prop:String = "";

		/**
		 * 动画的属性
		 * @return
		 *
		 */
		public function get prop():String {
			return _prop;
		}
		private var _begin:Number = 0;

		public function get begin():Number {
			return _begin;
		}

		public function set begin(value:Number):void {
			_begin = value;
			this.change = finish - begin;
		}

		private var _finish:Number = 0;

		public function get finish():Number {
			return _finish;
		}

		public function set finish(value:Number):void {
			_finish = value;
			this.change = finish - begin;
		}

		private var change:Number = 0;

		private var _position:Number = 0;

		public function get position():Number {
			return _position;
		}

		public var duration:Number = 0; //动画持续时间  millisecond

		/**
		 *
		 * @param target 动画作用的对象
		 * @param prop 动画作用的对象的属性的名字
		 * @param begin 开始值
		 * @param finish 结束值
		 *
		 */
		public function DTweenProp(prop:String, begin:Number, finish:Number, duration:Number, ease:Function = null) {
			this._prop = prop;

			this.begin = begin;
			this.finish = finish;
			this.duration = duration;

			this._position = begin;

			if (ease == null) {
				this.easeFun = easeDefault;
			} else {
				this.easeFun = ease;
			}
		}

		/**
		 * 反转播放
		 *
		 */
		public function back():void {
			var temp:Number = begin;
			begin = finish;
			finish = temp;

			this.change = finish - begin;
			this._position = begin;
		}

		/**
		 *
		 * @param	usedmillisecond
		 */
		public function update(usedmillisecond:Number):void {
			if (usedmillisecond >= duration) {
				this._completeRate = 1;
			} else {
				this._completeRate = easeFun(usedmillisecond, 0, 1, duration);
			}

			_position = begin + change * completeRate;
		}

	}

}
